package com.moorecat.app;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moorecat.R;
import com.moorecat.bean.Shop;
import com.moorecat.utils.ui.UiUtils;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-19 上午09:17:35 类说明
 */
public class BusinessAdapter extends BaseAdapter {

	private Context ctx;

	private List<Shop> shops;

    private boolean showDistance;

	public BusinessAdapter(Context ctx, List<Shop> shops, boolean showDistance) {
		this.ctx = ctx;
		this.shops = shops;
        this.showDistance = showDistance;
	}

	@Override
	public int getCount() {
		return shops.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		Shop shop = shops.get(position);
		View itemView = paramView;
		if (itemView == null) {
			itemView = (LinearLayout) LayoutInflater.from(ctx).inflate(
					R.layout.business_list_item, null);
		}
		// View itemView = (LinearLayout) LayoutInflater.from(ctx).inflate(
		// R.layout.business_list_item, null);
		TextView name = (TextView) itemView.findViewById(R.id.name);
		ImageView icon = (ImageView) itemView.findViewById(R.id.icon);
		ImageView youhuiquan = (ImageView) itemView.findViewById(R.id.coupon);
		ImageView star = (ImageView) itemView.findViewById(R.id.star);
		TextView type = (TextView) itemView.findViewById(R.id.type);
		TextView zifei = (TextView) itemView.findViewById(R.id.zifei);
		TextView rigion = (TextView) itemView.findViewById(R.id.rigion);
        TextView distance = (TextView) itemView.findViewById(R.id.distiance);
		name.setText(shop.getName());
		type.setText(shop.getCategoryName());
		if (shop.getAvgConsumption() < 1) {
			zifei.setVisibility(View.GONE);
		} else {
			zifei.setVisibility(View.VISIBLE);
			zifei.setText(ctx.getResources().getString(R.string.zifei_avg,
					shop.getAvgConsumption()));
		}
		rigion.setText(shop.getAddress().getAreaName());
		star.setBackgroundDrawable(UiUtils.selectStar(ctx.getResources(),
				shop.getRating()));
		icon.setImageBitmap(shop.getIcon());
		if (shop.isHasCoupon()) {
			youhuiquan.setVisibility(View.VISIBLE);
			youhuiquan.setBackgroundResource(R.drawable.seller_coupon);
		} else {
			youhuiquan.setVisibility(View.GONE);
		}
        if(this.showDistance){
            distance.setVisibility(View.VISIBLE);
            distance.setText(shop.getDistance()+"米");
        }else {
            distance.setVisibility(View.GONE);
        }
		return itemView;
	}
}
